import { isObject, isUnDef } from '@/utils/is';

/**
 * 修改对象中为undefined的字段，使其能在使用JSON.stringify时显示该字段
 * @param paramsData
 * @constructor
 */
export const fEmptyValue = (paramsData): Object => {
  function cldReset(data) {
    let temp = {};
    if (isObject(data) && Object.keys(data).length) {
      Object.keys(data).map(function (v) {
        if (isUnDef(data[v])) {
          temp[v] = '';
        } else {
          temp[v] = cldReset(data[v]);
        }
      });
    } else {
      temp = data;
    }
    return temp;
  }
  return cldReset(paramsData);
};

export const fIntNumber = (value): string | number => {
  if (typeof value === 'string') {
    return !isNaN(Number(value)) ? value.replace(/\./g, '') : 0;
  } else if (typeof value === 'number') {
    return !isNaN(value) ? String(value).replace(/\./g, '') : 0;
  } else {
    return 0;
  }
};

export const fIpNumber = (value: string | number): string | number => {
  return value;
};

export function fnFilterTree(data, searchStr) {
  return data.filter((item) => {
    if (item.name.includes(searchStr)) {
      return true;
    } else if (item.children) {
      item.children = fnFilterTree(item.children, searchStr);
      return item.children.length > 0;
    }
    return false;
  });
}
